package com.chenyu.util;

import java.sql.*;

/**
 * jdbc操作 自定义配置版，写死
 */
public  class JDBCUtil {
    private static Connection getConn() {
        String driver = "com.mysql.jdbc.Driver";
        String url = "jdbc:mysql://47.106.10.148:3306/test";
        String username = "root";
        String password = "root";
        Connection conn = null;
        try {
            Class.forName(driver); //classLoader,加载对应驱动
            conn = (Connection) DriverManager.getConnection(url, username, password);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }
    public static Integer getAll() {
        Connection conn = getConn();
        String sql = "select * from user";
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            pstmt = conn.prepareStatement(sql);
            rs = pstmt.executeQuery();
            int col = rs.getMetaData().getColumnCount();
            System.out.println("开始============================");
            while (rs.next()) {
                for (int i = 1; i <= col; i++) {
                    System.out.print(rs.getString(1) + "\t");
                }

            }
            System.out.println("结束===========================");
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                if(null != rs){
                    rs.close();
                }
               if(null != pstmt){
                   pstmt.close();
               }
               if(null != conn){
                   conn.close();
               }
            }catch (Exception ec){
                ec.printStackTrace();
            }
        }
        return null;
    }
}
